package main

import (
	"encoding/binary"
	"flag"
	"fmt"
	"io/ioutil"
	"net"
	"os"
)

func main() {
	flagset := flag.NewFlagSet("parameters", flag.ExitOnError)
	var sAddr string
	flagset.StringVar(&sAddr, "s", "", "server address, e.g. 127.0.0.1:3131")
	var dataFile string
	flagset.StringVar(&dataFile, "d", "", "the file that contains the data to be sent.")
	flagset.Parse(os.Args[1:])

	conn, err := net.Dial("tcp4", sAddr)
	if err != nil {
		panic(err)
	}
	data, err := ioutil.ReadFile(dataFile)
	fmt.Println(len(data))
	if err != nil {
		panic(err)
	}
	lenBytes := make([]byte, 4)
	binary.BigEndian.PutUint32(lenBytes, uint32(len(data)))
	conn.Write(lenBytes)
	n, err := conn.Write(data)
	fmt.Println(n, err)
	conn.Close()

}
